<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-calendar"></i>硬件管理</el-breadcrumb-item>
                <el-breadcrumb-item>硬件列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-form :inline="true" ref="formQuery" :model="formQuery" class="demo-form-inline" label-width="100px">
                    <el-form-item label="imei">
                        <el-input v-model="formQuery.imei" placeholder="imei"></el-input>
                    </el-form-item>
                    <el-form-item label="所属代理商" prop="agentId" >
                        <el-select v-model="formQuery.agentId" placeholder="请选择" @change="getAllMerData">
                            <el-option
                                v-for="item in agentList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="所属商户" prop="merchantId">
                        <el-select v-model="formQuery.merchantId" placeholder="请选择"  @change="changeMerData">
                            <el-option
                                v-for="item in merList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="联线状态" prop="onlineState">
                        <el-select v-model="formQuery.onlineState" placeholder="请选择">
                            <el-option key="0" label="在线" value="ON"></el-option>
                            <el-option key="1" label="离线" value="OFF"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="设备状态" prop="deviceStatus">
                        <el-select v-model="formQuery.deviceStatus" placeholder="请选择">
                            <el-option key="0" label="预制" value="Prepare"></el-option>
                            <el-option key="1" label="入库" value="IN"></el-option>
                            <el-option key="2" label="出库" value="OUT"></el-option>
                            <el-option key="3" label="正在使用" value="USEING"></el-option>
                            <el-option key="4" label="禁用" value="FORBIDDEN"></el-option>
                            <el-option key="5" label="故障" value="FAULT"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
                </el-form>
            </div>
            <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="handleAdd()">新增</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" :row-class-name="tableRowClassName">
                <el-table-column prop="imei" label="imei" align="center" width="150">
                </el-table-column>
                <el-table-column prop="agentName" label="所属代理商" width="120" align="center">
                </el-table-column>
                <el-table-column prop="merchantName" label="所属商户" width="200" align="center">
                </el-table-column>
                <el-table-column prop="active" label="在线状态" :formatter="filterActive" width="100" align="center">
                </el-table-column>
                <el-table-column prop="status" label="设备情况" :formatter="filterStatus" width="100" align="center">
                </el-table-column>
                <el-table-column prop="createTime" :formatter="filterDate" label="创建时间" width="120" align="center">
                </el-table-column>
                <el-table-column prop="qrFileName" label="二维码" width="70" align="center">
                    <template slot-scope="scope">
                      <el-popover trigger="click" placement="top">
                        <img class="qr-code-large" :src="imgPath + scope.row.qrFileName"/>
                        <div slot="reference" class="qr-code">
                          <img :src="imgPath + scope.row.qrFileName"/>
                        </div>
                      </el-popover>
                    </template>
                </el-table-column>
                <el-table-column prop="lastTradeYmd" label="最后交易日" width="120" align="center">
                </el-table-column>
                <el-table-column prop="lastTradeId" label="最近一笔订单" width="120" align="center">
                  <template slot-scope="scope">
                    <el-button @click="handleQuery(scope.row)" type="text" size="small">{{scope.row.lastTradeId}}</el-button>
                  </template>
                </el-table-column>
                <el-table-column
                  fixed="right"
                  label="操作">
                  <template slot-scope="scope">
                    <el-button @click="handleQuery(scope.row)" type="text" size="small">查看</el-button>
                    <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
                    <!-- <el-button @click="handleCheckin(scope.row)" type="text" size="small">签收</el-button>
                    <el-button @click="handleDis(scope.$index, scope.row)" type="text" size="small">分配</el-button> -->

                  </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next" :total="pageData.total" :page-size="pageData.pageSize">
                </el-pagination>
            </div>
        </div>
        <!-- 分配弹出框 -->
        <el-dialog title="分配" :visible.sync="disVisible" width="30%">
            <el-form ref="disform" :model="disform" label-width="100px">
                <el-select v-model="disform.merchantId" placeholder="请选择">
                    <el-option
                        v-for="item in merList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="disVisible = false">取 消</el-button>
                <el-button type="primary" @click="disHardware">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%">
            <el-form ref="ruleForm" :model="ruleForm" label-width="100px">
                <el-form-item label="imei" prop="imei">
                    <el-input v-model="ruleForm.imei"></el-input>
                </el-form-item>
                <el-form-item label="所属代理商" prop="agentId">
                    <el-select v-model="ruleForm.agentId" placeholder="请选择">
                        <el-option
                            v-for="item in agentList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </span>
        </el-dialog>
    </div>


</template>
<script>
    import moment from 'moment'
    import BW from '../../../assets/scripts/common/bwCommon'

    export default {
        name: 'basetable',
        data() {
            return {
                agentList: [],    // 代理商列表
                merList: [], // 商户列表
                tableData: [],
                pageData: {
                    total: 0,
                    pageSize: 10,
                },
                formQuery: {
                    pageNo: 1,      // 当前页数
                    IMEI: '',
                    onlineState: '',
                    merchantId:'',
                    agentId:'',
                    deviceStatus:''
                },
                disform:{
                    merchantId:'',
                    agentId:'',
                    id:'',
                },
                ruleForm: {
                  imei: '',
                  agentId:'',
                },
                rules: {
                  imei: [
                    { required: true, message: '请输入imei', trigger: 'blur' },
                  ],
                  agentId: [
                    { required: true, message: '请选择所属代理商', trigger: 'blur' }
                  ]
                },
                disVisible:false, // 分配弹框
                addVisible:false, // 新增弹框
                imgPath: `${WEBCONFIG.SOURCE_HOST_PROD}/device/`
            }
        },
        created() {

        },
        mounted() {
            this.getAllData()
        },
        computed: {
            // data() {
            //     return this.tableData.filter((d) => {
            //         let is_del = false;
            //         for (let i = 0; i < this.del_list.length; i++) {
            //             if (d.name === this.del_list[i].name) {
            //                 is_del = true;
            //                 break;
            //             }
            //         }
            //         if (!is_del) {
            //             return d;
            //         }
            //     })
            // }
        },
        methods: {
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 分页导航
            handleCurrentChange(val) {
                this.formQuery.pageNo = val;
                this.getData();
            },
            async getAllData() {
                let res1 = await this.request('allAgentList')
                this.agentList = res1.data
                this.formQuery.agentId = res1.data[0].id
                this.getData()
            },
            async getAllMerData(agentId) {
                const param ={
                  agentId
                }
                let res1 = await this.request('allMerchantList',param, 'get')
                this.merList = res1.data
                this.formQuery.merchantId = res1.data[0].id
                this.getData()
            },
            async getData() {   // 获取数据
                const param = {
                    pageNo:this.formQuery.pageNo
                }
                if (process.env.NODE_ENV === 'development') {
                    this.imgPath = `${WEBCONFIG.SOURCE_HOST}/device/`
                };
                let res = await this.request('hardwareList', param)
                this.tableData = res.data.list
                this.pageData = {
                    total: res.data.total,
                    pageSize: res.data.pageSize
                }
            },
            changeMerData(val) {

            },
            submitForm() {
                this.$refs.ruleForm.validate(async (valid) => {
                  if (valid) {
                    await this.request('hardwareAdd', this.ruleForm);
                    BW.Toast('新增成功!');
                    this.addVisible = false;
                    this.getData()
                  } else {
                    console.log('error submit!!');
                    return false;
                  }
                });
              },
            async onSubmit() {
                if (process.env.NODE_ENV === 'development') {
                    this.imgPath = `${WEBCONFIG.SOURCE_HOST}/device/`
                };
                let res = await this.request('hardwareQuery', this.formQuery)
                this.tableData = res.data.list
                this.pageData = {
                    total: res.data.total,
                    pageSize: res.data.pageSize
                }
            },
            handleAdd() {
                this.addVisible = true;
            },
            handleQuery(val) {
                const record = val.imei;
                this.$router.push(`/hardwareInfo?action=query&id=${record}`);
            },
            handleEdit(val) {
                const record = val.imei;
                this.$router.push(`/hardwareInfo?action=edit&id=${record}`);
            },
            handleDis(index, val) { // 分配弹框
                this.disform.id = val.id;
                this.disform.agentId = val.agentId;
                this.getAllMerData(val.agentId)
                this.disVisible = true;
            },
            async disHardware() {   // 分配
                let res = await this.request('hardwareDis', this.disform)
                this.disVisible = false;
                this.$message({
                    type: 'success',
                    message: '分配成功!'
                });
            },
            handleCheckin(val) {
                const param ={
                    agentId:val.agentId,
                    id:val.id
                }
                this.$confirm('请确认签收, 是否继续?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  this.signHardware(param)
                }).catch(() => {
                  this.$message({
                    type: 'info',
                    message: '已取消签收'
                  });
                });
                // BW.sureToast('请确认签收？', param => this.signHardware(param));
            },
            async signHardware(param) {   // 签收
                let res = await this.request('hardwareCheckin', param)
                this.$message({
                    type: 'success',
                    message: '签收成功!'
                });
            },
            filterDate(row, column) {
                let time = moment(row.creatTime).format('YYYY-MM-DD')
                return time;
            },
            filterActive(row, column) {
                let txt = '';
                if(row.active === 'OFF'){
                    txt = '离线'
                } else if(row.active === 'ON'){
                    txt = '在线'
                }
                return txt;
            },
            filterStatus(row, column) {
                let txt = '';
                if(row.status === 'Prepare'){
                    txt = '预制'
                } else if(row.status === 'IN'){
                    txt = '入库'
                } else if(row.status === 'OUT'){
                    txt = '出库'
                } else if(row.status === 'USEING'){
                    txt = '正在使用'
                } else if(row.status === 'FORBIDDEN'){
                    txt = '禁用'
                }else if(row.status === 'FAULT'){
                    txt = '故障'
                }
                return txt;
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
    .el-table .warning-row {
        background: oldlace;
    }

    .el-table .success-row {
        background: #f0f9eb;
    }

    .qr-code-large{
      width: 300px;
      height: 300px;
    }

    .qr-code img{
      width: 30px;
      height: 30px;
    }
</style>
